var path = new Map();
var url = require('url');
var fs = require('fs');
var respUtil = require("../util/RespUtil");
var fileListDao = require('../dao/fileListDao');

function getPic(request,response) {

    var params = url.parse(request.url, true).query;

    try {
        var data = fs.readFileSync('./' + params.path);

        response.writeHead(200);
        response.write(data);
        response.end();

    } catch (e) {
        response.writeHead(404);
        response.end();
    }

}
path.set("/getPic",getPic);

function getAllImg(request,response) {
    var params = url.parse(request.url, true).query;
    fileListDao.getAllFileList(parseInt(params.page), parseInt(params.pageSize),function (result) {
        response.writeHead(200,respUtil.writeHeadChinese());
        response.write(respUtil.writeResult("success", "查询成功", result));
        response.end();
    });
}
path.set("/getAllImg",getAllImg);

function delImg(request,response) {

    var ids = request.body.ids || 0;
    var imgPathArr = request.body.imgPath || "";

    fileListDao.delFileList(ids,function (result) {
        for (var i=0;i<imgPathArr.length;i++){
            fs.unlinkSync('./' + imgPathArr[i]);
        }
        response.writeHead(200,respUtil.writeHeadChinese());
        response.write(respUtil.writeResult("success", "删除成功", result));
        response.end();
    });

}
path.set("/delImg",delImg);

function queryFileListCount(request,response) {
    fileListDao.queryFileListCount(function (result) {
        response.writeHead(200,respUtil.writeHeadChinese());
        response.write(respUtil.writeResult("success", "查询成功", result));
        response.end();
    });

}
path.set("/queryFileListCount",queryFileListCount);




module.exports.path = path;